/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.controller;

import com.alibaba.druid.sql.visitor.functions.Bin;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.jckj.core.annotation.LogTitle;
import com.jckj.core.contants.SysContants;
import com.jckj.core.entity.AgriProView;
import com.jckj.core.entity.AgriProduct;
import com.jckj.core.enums.SysDictType;
import com.jckj.core.service.AgriProductService;
import com.jckj.core.service.SysDictService;
import com.jckj.core.util.ClassUtils;
import com.jckj.core.util.StringUtils;
import com.jckj.core.validator.groups.Add;
import com.jckj.core.validator.groups.Delete;
import com.jckj.core.validator.groups.Edit;
import com.jckj.core.vos.*;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Calendar;

/**
 * <p>
 * </p>
 *
 * @author 刘志远
 * @date 18-3-1
 */
@Controller
@RequestMapping("/admin")
public class AgriProController {

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private AgriProductService agriProductService;

    @Autowired
    private LoginUserVo loginUserVo;

    @RequestMapping(value = "/agriProList", method = RequestMethod.GET)
    public ModelAndView farmParkList(){
        ModelAndView modelAndView = new ModelAndView("agriProList");
        modelAndView.addObject("proTpyeList", sysDictService.getSysDictListBy(SysDictType.PRODUCT_TYPE.getTypeCode()));
        return modelAndView;
    }

    @RequestMapping(value = "/getAgriProList", method = RequestMethod.GET)
    @ResponseBody
    public LayPageVo getAgriProList(AgriProductQueryVo vo){
        if(vo.getLimit()==null||vo.getLimit()==0){
            vo.setLimit(SysContants.LIMIT);
        }
        if(vo.getPage()==null||vo.getPage()==0){
            vo.setPage(1);
        }
        PageInfo<AgriProView> pageInfo = agriProductService.findAgriProViewByPage(vo);
        LayPageVo layPageVo = new LayPageVo();
        layPageVo.setCode(0);
        layPageVo.setCount(pageInfo.getTotal());
        layPageVo.setData(pageInfo.getList());
        return layPageVo;
    }

    @RequestMapping(value = "/addAgriPro", method = RequestMethod.GET)
    public ModelAndView addAgriPro(){
        ModelAndView modelAndView = new ModelAndView("addAgriPro");
        modelAndView.addObject("proTpyeList", sysDictService.getSysDictListBy(SysDictType.PRODUCT_TYPE.getTypeCode()));
        return modelAndView;
    }

    /**
     * 检测产品编号是否重复
     * @param proCode
     * @return
     */
    @RequestMapping(value = "/checkProCode", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkProCode(String proCode){
        if(StringUtil.isEmpty(proCode)){
            return true;
        }
        return agriProductService.checkProCode(proCode);
    }

    /**
     * 检测产品名称编号
     * @param proName
     * @return
     */
    @RequestMapping(value = "/checkProName", method = RequestMethod.GET)
    @ResponseBody
    public boolean checkProName(String proName){
        if(StringUtil.isEmpty(proName)){
            return true;
        }
        return agriProductService.checkProNmae(proName);
    }

    @RequestMapping(value = "/saveAgriPro", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "增加农产品")
    public Result saveAgriPro(@ModelAttribute @Validated(value = Add.class) AgriProductVo agriProductVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        boolean proCodeExists = agriProductService.checkProCode(agriProductVo.getProCode());
        if(!proCodeExists){
            return Result.error("产品编号重复");
        }
        boolean proNameExists = agriProductService.checkProNmae(agriProductVo.getProName());
        if(!proNameExists){
            return Result.error("产品名称重复");
        }
        AgriProduct agriProduct= new AgriProduct();
        try {
            ClassUtils.copyValue(agriProductVo, agriProduct);
        } catch (Exception e) {
            e.printStackTrace();
        }
        agriProduct.setId(StringUtils.genId());
        agriProduct.setCreateBy(loginUserVo.getId());
        agriProduct.setCreateTime(Calendar.getInstance().getTime());
        agriProduct.setIsSys(true);
        agriProductService.saveAgriPro(agriProduct);
        return Result.ok();
    }

    @RequestMapping(value = "/editAgriPro", method = RequestMethod.GET)
    public ModelAndView editAgriPro(@ModelAttribute @Validated(Edit.class) AgriProductVo agriProductVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()) {
            return new ModelAndView("404");
        }
        AgriProduct agriProduct = agriProductService.findAgriProductById(agriProductVo.getId());
        if(agriProduct==null){
            return new ModelAndView("404");
        }
        ModelAndView modelAndView = new ModelAndView("editAgriProduct");
        modelAndView.addObject("agriProduct", agriProduct);
        return modelAndView;
    }

    @RequestMapping(value = "/delAgriPro", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "删除农产品")
    public Result delAgriPro(@ModelAttribute @Validated(Delete.class) AgriProductVo agriProductVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        boolean hasRef =  agriProductService.hasRef(agriProductVo.getId());
        if(hasRef){
            return Result.error("该数据存在关联不能删除");
        }
        agriProductService.delAgriPro(agriProductVo.getId());
        return Result.ok();
    }


}
